#!/usr/bin/python
# -*- coding: utf-8 -*-
#import OF3D_PublicClass.toolkits as ftks
#reload(ftks)
#ftk = ftks.Toolkits()

__version__ = "1.0.1"
"""
#up data note:
#v1.0.1 2012/06/08
"""

import maya.cmds as mc
import maya.mel as mel
import re
from maya import OpenMaya as om
import maya.OpenMayaAnim as oma
import OF3D_PublicClass.OF3D_zchPipePublicClass as PublicClass
import OF3D_PublicClass.searchByName as sbn

class Toolkits(PublicClass.PublicClass,sbn.Matching):
    def __init__(self):
        pass
    def putObjectOnCurve(self,*curves,**flags):
        """OF3D_PublicClass.toolkits->putObjectOnCurve
        describe:
        """
        defineFlag = {"count":(1,int),
                      "printResult":(True,bool),
                      "start":(0.0,float),
                      "end":(1.0,float),
                      "objType":("joint",str),
                      "name":("objectOnCurve",str)
        }
        flag = self.funtionFlag(defineFlag, **flags)
        #read value 
        objType =  flag["objType"]
        count = abs( flag["count"] )
        printRt = flag["printResult"]
        start = flag["start"]
        end = flag["end"]
        name = flag["name"]
        #get curves
        if len(curves)==0:
            #if no input get from selected
            curves = mc.ls(sl=True,ap=True)
        if len(curves)==0:
            self.mayaError( "No curve selected or no curve input //" )
            return
        #
        result = []
        divide = (end-start)/(count-1.0)
        for curve in curves:
            itsShape = mc.listRelatives(curve,shapes=True,fullPath=True)
            if itsShape==None:#no shape object
                continue
            isACurve = mc.objectType(itsShape[0], isType="nurbsCurve" )
            if not isACurve:#not a nurbs curve
                continue
            parameter = start
            for i in range(1,count+1,1):
                obj = mc.createNode( objType,name = "%s%s"%(name,i) )
                poci = mc.createNode( "pointOnCurveInfo", name = "%s%s_poci"%(name,i) )
                mc.setAttr(".turnOnPercentage",1)
                mc.setAttr(".parameter",parameter)
                mc.connectAttr( "%s.worldSpace"%( itsShape[0] ),"%s.inputCurve"%( poci ) )
                mc.connectAttr("%s.positionX"%(poci), "%s.translateX"%(obj) )
                mc.connectAttr("%s.positionY"%(poci), "%s.translateY"%(obj) )
                mc.connectAttr("%s.positionZ"%(poci), "%s.translateZ"%(obj) )
                parameter += divide
                result.append(obj)
        return result
    def closestPointOnCurve(self,objOrWsT,curveName):
        """get closest point on curve and paramAtPoint
        
        #objOrWsT an object name or a world position list
        #curveName the curve transform name;
        return positon parameter
        """
        if type( objOrWsT )==list:
            wsPosit = objOrWsT
        elif type( objOrWsT )==str and mc.objExists( objOrWsT ):
            wsPosit = mc.xform(objOrWsT,q=True,ws=True,t=True)
        else:
            raise "TypeError","The first argument must n object name or a world position list //"
        
        itShapes = mc.listRelatives(curveName,shapes=True,type="nurbsCurve")
        if itShapes==None:
            raise "TypeError","No one nurbsCurve input //"
        
        curveShapeName = itShapes[0]
        mdagPath = self.getDagByName(curveShapeName)
        curveFn = om.MFnNurbsCurve(mdagPath)
        
        mpoint = om.MPoint(wsPosit[0],wsPosit[1],wsPosit[2],1.0)
        nearestPoint = curveFn.closestPoint(mpoint,None,0.001,om.MSpace.kWorld)
        u_util = om.MScriptUtil()
        uPtr = u_util.asDoublePtr()
        curveFn.getParamAtPoint(nearestPoint,uPtr,om.MSpace.kWorld)
        u = u_util.getDouble(uPtr)
        #mc.setAttr("%s.parameter"%ptOnCurveInfo , u )
        return [nearestPoint.x,nearestPoint.y,nearestPoint.z],u
        
        
        
        
        
        
        
        
        
        
        
        